<?php 
class ControllerCatalogAttribute extends Controller {
	private $error = array();
 
	public function index() {
		$this->load->language('catalog/attribute');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/attribute');
		
		$this->getList();
	}

	public function insert() {
		$this->load->language('catalog/attribute');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/attribute');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_catalog_attribute->addAttribute($this->request->post);  			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';    			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page']; 
			}     
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}   
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . $url);
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('catalog/attribute');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/attribute');  		
        
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {            
			$this->model_catalog_attribute->editAttribute($this->request->get['attribute_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';   			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}      
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}   
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
					
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . $url);
		}

		$this->getForm();
	}

	public function delete() {
		$this->load->language('catalog/attribute');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/attribute');
		
		if (isset($this->request->post['selected'])) {
			foreach ($this->request->post['selected'] as $attribute_id) {
                if($this->validateDelete($attribute_id)){
				    $this->model_catalog_attribute->deleteAttribute($attribute_id);
                }
			}
			
			$this->session->data['success'] = $this->language->get('text_delete_success');

			$url = '';   			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}  
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}  
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . $url);
		}

		$this->getList();
	}

	private function getList() {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} 
        else {
			$page = 1;
		}         	
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} 
        else {
			$sort = 'sort_order';
		}          		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} 
        else {
			$order = 'ASC';
		}

		$url = '';   	
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}        		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}  
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();       
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);  
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/insert&token=' . $this->session->data['token'] . $url;
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/delete&token=' . $this->session->data['token'] . $url;
		
		$this->data['attributes'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		); 		
		
		$results = $this->model_catalog_attribute->getAttributes($data);      
		foreach ($results as $result) {
			$action = array();			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=catalog/attribute/update&token=' . $this->session->data['token'] . '&attribute_id=' . $result['attribute_id'] . $url
			);
						
			$this->data['attributes'][] = array(
				'attribute_id'  => $result['attribute_id'],
				'name'          => $result['name'],
                'description'   => $result['description'], 
				'sort_order'    => $result['sort_order'],	
                'status'        => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),			
				'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
				'selected'      => isset($this->request->post['selected']) && in_array($result['attribute_id'], $this->request->post['selected']),
				'action'        => $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');    
		$this->data['text_no_results'] = $this->language->get('text_no_results');
         
		$this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_description'] = $this->language->get('column_description');       
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
        $this->data['column_status'] = $this->language->get('column_status'); 
		$this->data['column_date_modified'] = $this->language->get('column_date_modified');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = ''; 
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} 
        else {
			$url .= '&order=ASC';
		} 
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_name'] = HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . '&sort=name' . $url;
		$this->data['sort_sort_order'] = HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . '&sort=sort_order' . $url;		
        $this->data['sort_status'] = HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . '&sort=status' . $url;        
		$this->data['sort_date_modified'] = HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . '&sort=date_modified' . $url;
		
		$attribute_total = $this->model_catalog_attribute->getTotalAttributes();
        $url = '';                                                                    
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}  												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}  
		$pagination = new Pagination();
		$pagination->total = $attribute_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . $url . '&page={page}';
			
		$this->data['pagination'] = $pagination->render(); 		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		
		$this->template = 'catalog/attribute_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');
    	
		$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
        $this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_status'] = $this->language->get('entry_status');  	
        $this->data['entry_is_color'] = $this->language->get('entry_is_color');      

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
        $this->data['button_remove'] = $this->language->get('button_remove');  
        $this->data['button_add'] = $this->language->get('button_add');  

		$this->data['tab_general'] = $this->language->get('tab_general');
        $this->data['text_attribute_value'] = $this->language->get('text_attribute_value');  
        $this->data['text_value_name'] = $this->language->get('text_value_name');  
        $this->data['text_value_sort'] = $this->language->get('text_value_sort');  
        $this->data['text_value_color'] = $this->language->get('text_value_color');  
        $this->data['text_image_manager'] = $this->language->get('text_image_manager');
        
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} 
        else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} 
        else {
			$this->data['error_name'] = '';
		}   	
        if (isset($this->error['description'])) {
            $this->data['error_description'] = $this->error['description'];
        } 
        else {
            $this->data['error_description'] = '';
        }           	
 		if (isset($this->error['sort_order'])) {
			$this->data['error_sort_order'] = $this->error['sort_order'];
		} 
        else {
			$this->data['error_sort_order'] = '';
		}          
        if (isset($this->error['attribute_values'])) {
            $this->data['error_attribute_values'] = $this->error['attribute_values'];
        } 
        else {
            $this->data['error_attribute_values'] = '';
        }
		
		$url = ''; 			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}  		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		} 
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);    
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		
		if (!isset($this->request->get['attribute_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/insert&token=' . $this->session->data['token'] . $url;
		} 
        else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/update&token=' . $this->session->data['token'] . '&attribute_id=' . $this->request->get['attribute_id'] . $url;
		} 				
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=catalog/attribute&token=' . $this->session->data['token'] . $url;

        $attribute_id = isset($this->request->get['attribute_id']) ? (int)$this->request->get['attribute_id'] : 0;
		if ($attribute_id && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$attribute_info = $this->model_catalog_attribute->getAttribute($attribute_id);            
		}

		if (isset($this->request->post['name'])) {
			$this->data['name'] = $this->request->post['name'];
		} 
        elseif (isset($attribute_info)) {
			$this->data['name'] = $attribute_info['name'];
		} 
        else {
			$this->data['name'] = '';
		}         

        if (isset($this->request->post['description'])) {
            $this->data['description'] = $this->request->post['description'];
        } 
        elseif (isset($attribute_info)) {
            $this->data['description'] = $attribute_info['description'];
        } 
        else {
            $this->data['description'] = '';
        }         
        
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} 
        elseif (isset($attribute_info)) {
			$this->data['sort_order'] = $attribute_info['sort_order'];
		} 
        else {
			$this->data['sort_order'] = '';
		}

    	if (isset($this->request->post['status'])) {
      		$this->data['status'] = $this->request->post['status'];
    	} 
        elseif (isset($attribute_info)) {
			$this->data['status'] = $attribute_info['status'];
		} 
        else {
      		$this->data['status'] = '';
    	}
		
        if (isset($this->request->post['is_color_group'])) {
              $this->data['is_color_group'] = $this->request->post['is_color_group'];
        } 
        elseif (isset($attribute_info)) {
            $this->data['is_color_group'] = $attribute_info['is_color_group'];
        } 
        else {
              $this->data['is_color_group'] = 0;
        }
        
        $attribute_values = array();
        if (isset($this->request->post['attribute_values'])) {
            $attribute_values = $this->request->post['attribute_values'];
        } 
        else {
            $attribute_values = $this->model_catalog_attribute->getAttributeValues($attribute_id); 
        }            
        $av_list = array();  
        $this->load->model('tool/image');       
        foreach($attribute_values as &$item){
            if($item['image'] && file_exists(DIR_IMAGE . $item['image'])) {
                $image_file = $item['image'];  
                $preview = $this->model_tool_image->resizeNoScale($image_file, 100, 20);  
            } 
            else {
                $image_file = 'no_image.jpg';
                $preview = $this->model_tool_image->resize($image_file, 100, 20);  
            }
            
            $item['image_preview'] = array(
                'preview'       => $preview,
                'file'          => $image_file,
            );        
        }     
        
        $this->data['attribute_values'] = $attribute_values;
        $this->data['token'] = $this->session->data['token'];
        $this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 20);
             
		$this->template = 'catalog/attribute_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	private function validateForm() {         
		if (!$this->user->hasPermission('modify', 'catalog/attribute')) { 
			$this->error['warning'] = $this->language->get('error_permission');
		} 

		if ((strlen(utf8_decode($this->request->post['name'])) < 1) || (strlen(utf8_decode($this->request->post['name'])) > 255)) {
			$this->error['name'] = $this->language->get('error_name');
		}

        if ((strlen(utf8_decode($this->request->post['description'])) < 1) || (strlen(utf8_decode($this->request->post['description'])) > 255)) {
            $this->error['description'] = $this->language->get('error_description');
        }
        
        if (!is_numeric(utf8_decode($this->request->post['sort_order']))) {
            $this->error['sort_order'] = $this->language->get('error_sort_order');
        }
        
        if(isset($this->request->post['attribute_values'])){
            $attribute_values = $this->request->post['attribute_values'];
            foreach($attribute_values as $item){
                if ((strlen(utf8_decode($item['name'])) < 1) || (strlen(utf8_decode($item['name'])) > 255) || !is_numeric(utf8_decode($item['sort_order']))) { 
                    $this->error['attribute_values'] = $this->language->get('error_attribute_values');  
                    break;  
                }   
            }
        }
        
		if (!$this->error) { 
			return TRUE;
		} 
        else {
			return FALSE;
		}
	}

	private function validateDelete($attribute_id) {
		if (!$this->user->hasPermission('modify', 'catalog/attribute')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

        if (!$this->model_catalog_attribute->canDelete($attribute_id)) {
            $this->error['warning'] = $this->language->get('error_delete');
        }
        
		if (!$this->error) {
			return TRUE;
		} 
        else {
			return FALSE;
		}
	}	
}
?>